package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.NavService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements NavService {
    @Override
    public List<NavModel> getNavModelList() {
        List<NavModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("hrefList");
        if (isHave) {
//从redis里面去取数据
            List<String> hrefList = jedis.lrange("hrefList", 0, jedis.llen("hrefList"));
            List<String> titleList = jedis.lrange("titleList", 0, -1);
            for (int i = 0; i < hrefList.size(); i++) {
                NavModel navModel = new NavModel();
                navModel.setHref(hrefList.get(i));
                navModel.setTitle(titleList.get(i));
                list.add(navModel);
            }
            System.out.println("redis");
        } else {
//            从数据库里面查询，然后将数据存入到redis
            NavDao navDao = new NavImpl();
            list = navDao.getNavList();
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("hrefList", list.get(i).getHref());
                jedis.rpush("titleList", list.get(i).getTitle());
            }
            System.out.println("mysql");
        }
        return list;
    }
}
